package com.letv.core.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.letv.core.bean.HomePageBean.Booting;
import com.letv.core.constant.DatabaseConstant;
import com.letv.core.constant.LetvConstant;
import com.letv.core.utils.LetvDateFormat;
import com.letv.core.utils.LetvTools;

public class FestivalImageTraceHandler {

	private Context context;

	public FestivalImageTraceHandler(Context context) {
		this.context = context;
	}

	public boolean hasFestivalImage(String name) {
		Cursor cursor = null;
		try {
			cursor = context.getContentResolver().query(LetvContentProvider.URI_FESTIVALIMAGE, new String[] { "count(*)" },
					DatabaseConstant.FestivalImageTrace.Field.NAME + " = ?", new String[] { name }, null);

			if (cursor.moveToFirst()) {

				int count = cursor.getInt(0);

				return count > 0 ? true : false;
			}

			return false;
		} finally {
			LetvTools.closeCursor(cursor);
		}
	}

	/**
	 * 删除一条
	 * */
	// public void delete(String name) {
	// context.getContentResolver().delete(LetvContentProvider.URI_FESTIVALIMAGE,
	// LetvConstant.DataBase.FestivalImageTrace.Field.NAME + " = ?",
	// new String[] { name });
	// }

	/**
	 * 清空数据库
	 * */
	public void clear() {
		context.getContentResolver().delete(LetvContentProvider.URI_FESTIVALIMAGE, null, null);
	}

	/**
	 * 批量插入
	 * */
	public void insert(List<Booting> bootings) {
		if (bootings != null && bootings.size() >= 0) {
			for (Booting b : bootings) {
				insert(b);
			}
		}
	}

	/**
	 * 插入一条
	 * */
	public void insert(Booting booting) {

		ContentValues cv = new ContentValues();

		cv.put(DatabaseConstant.FestivalImageTrace.Field.NAME, booting.getName());

		cv.put(DatabaseConstant.FestivalImageTrace.Field.PIC, booting.getPic());

		cv.put(DatabaseConstant.FestivalImageTrace.Field.START_TIME, booting.getPushpic_starttime());

		cv.put(DatabaseConstant.FestivalImageTrace.Field.END_TIME, booting.getPushpic_endtime());

		cv.put(DatabaseConstant.FestivalImageTrace.Field.ORDER, booting.getOrder());

		context.getContentResolver().insert(LetvContentProvider.URI_FESTIVALIMAGE, cv);

	}

	/**
	 * 更新开机图（一张）
	 */
	public void updateTime(String name, String pic, String startTime, String endTime, int order) {

		ContentValues cv = new ContentValues();

		cv.put(DatabaseConstant.FestivalImageTrace.Field.START_TIME, startTime);

		cv.put(DatabaseConstant.FestivalImageTrace.Field.END_TIME, endTime);

		cv.put(DatabaseConstant.FestivalImageTrace.Field.PIC, pic);

		cv.put(DatabaseConstant.FestivalImageTrace.Field.ORDER, order);

		context.getContentResolver().update(LetvContentProvider.URI_FESTIVALIMAGE, cv, DatabaseConstant.FestivalImageTrace.Field.NAME + " = ?",
				new String[] { name });
	}

	/**
	 * 得到当前应该显示的开机图
	 * */
	public Booting getCur() {
		int order = PreferencesManager.getInstance().getCurBootingOrder();
		order++;

		Cursor cursor = null;
		Booting booting = null;
		try {
			List<Booting> bootings = null;
			if (order == 1) {
				StringBuilder sb = new StringBuilder();
				sb.append(DatabaseConstant.FestivalImageTrace.Field.ORDER + " = ? ");

				cursor = context.getContentResolver().query(LetvContentProvider.URI_FESTIVALIMAGE, null, sb.toString(), new String[] { "1" }, null);
				bootings = build(cursor);
			} else {
				StringBuilder sb = new StringBuilder();
				sb.append(DatabaseConstant.FestivalImageTrace.Field.ORDER + " = ? OR ");
				sb.append(DatabaseConstant.FestivalImageTrace.Field.ORDER + " = ? ");

				cursor = context.getContentResolver().query(LetvContentProvider.URI_FESTIVALIMAGE, null, sb.toString(), new String[] { order + "", "1" }, null);
				bootings = build(cursor);
			}

			if (bootings != null) {
				if (bootings.size() == 1) {
					booting = bootings.get(0);
					order = 1;
				} else if (bootings.size() > 1) {
					for (Booting b : bootings) {
						if (b.getOrder() == order) {
							booting = b;
						}
					}
					if (booting == null) {
						booting = bootings.get(0);
						order = 1;
					}
				}
			}

			if (booting != null) {
				PreferencesManager.getInstance().setCurBootingOrder(order);
			}

			return booting;
		} finally {
			LetvTools.closeCursor(cursor);
		}
	}

	// public List<Booting> getOtherFestivalImages(String[] names) {
	//
	// Cursor cursor = null;
	// try {
	// if (names != null && names.length > 0) {
	// StringBuilder sb = new StringBuilder();
	// for (int i = 0; i < names.length; i++) {
	// if (i == names.length - 1) {
	// sb.append(LetvConstant.DataBase.FestivalImageTrace.Field.NAME +
	// " <> ? ");
	// } else {
	// sb.append(LetvConstant.DataBase.FestivalImageTrace.Field.NAME +
	// " <> ? AND ");
	// }
	// }
	//
	// cursor =
	// context.getContentResolver().query(LetvContentProvider.URI_FESTIVALIMAGE,
	// null, sb.toString(), names, null);
	// return build(cursor);
	// }
	// return null;
	// } finally {
	// LetvTools.closeCursor(cursor);
	// }
	// }
	//
	// public List<Booting> getDemodedFestivalImages(long currentTime) {
	// Cursor cursor = null;
	// try {
	// cursor =
	// context.getContentResolver().query(LetvContentProvider.URI_FESTIVALIMAGE,
	// null,
	// LetvConstant.DataBase.FestivalImageTrace.Field.END_TIME + " < ? ", new
	// String[] { currentTime + "" }, null);
	// return build(cursor);
	// } finally {
	// LetvTools.closeCursor(cursor);
	// }
	// }
	//
	// public List<Booting> getAvailableFestivalImages(long currentTime) {
	// Cursor cursor = null;
	// try {
	// cursor = context.getContentResolver()
	// .query(LetvContentProvider.URI_FESTIVALIMAGE,
	// null,
	// LetvConstant.DataBase.FestivalImageTrace.Field.START_TIME + " < ? AND " +
	// LetvConstant.DataBase.FestivalImageTrace.Field.END_TIME
	// + " > ? ", new String[] { currentTime + "", currentTime + "" }, null);
	//
	// return build(cursor);
	// } finally {
	// LetvTools.closeCursor(cursor);
	// }
	// }

	private List<Booting> build(Cursor cursor) {
		if (cursor == null) {
			return null;
		}

		List<Booting> bootings = new ArrayList<Booting>();

		while (cursor.moveToNext()) {

			Booting booting = new Booting();

			booting.setName(cursor.getString(cursor.getColumnIndex(DatabaseConstant.FestivalImageTrace.Field.NAME)));

			booting.setPic(cursor.getString(cursor.getColumnIndex(DatabaseConstant.FestivalImageTrace.Field.PIC)));

			booting.setPushpic_starttime((cursor.getString(cursor.getColumnIndex(DatabaseConstant.FestivalImageTrace.Field.START_TIME))));

			booting.setPushpic_endtime(cursor.getString(cursor.getColumnIndex(DatabaseConstant.FestivalImageTrace.Field.END_TIME)));

			booting.setOrder(cursor.getInt(cursor.getColumnIndex(DatabaseConstant.FestivalImageTrace.Field.ORDER)));

			//2014-02-10 ljnalex modify start 开机图片增加过期时间判断
			if (System.currentTimeMillis() < LetvDateFormat.getTimeInMillis(booting.getPushpic_endtime())
				&& System.currentTimeMillis() > LetvDateFormat.getTimeInMillis(booting.getPushpic_starttime())) {
				bootings.add(booting);
			}
			//2014-02-10 ljnalex modify end 开机图片增加过期时间判断
		}

		return bootings;
	}

}
